/**
 * 分类服务
 */
const { validationResult } = require("express-validator");
const { CODE_Conflict } = require("@root/config/index");
const { poolPromise } = require("@root/db/index");
const { validateParams } = require("@root/utils/peak-tools.js");

/**
 * 获取分类
 * @param {*} req
 * @param {*} res
 * @param {*} next
 */
async function selectCategoryList(req, res, next) {
  if (validateParams({ req, res, validationResult })) return;
  try {
    const select_category_sql = `select c.*, count(a.cate_id) as article_count from category c left join article a on c.cate_id = a.cate_id group by c.cate_id`;
    let [list] = await poolPromise.query(select_category_sql);
    res.status(200).json({
      code: 200,
      msg: "获取分类成功",
      data: list,
    });
  } catch (error) {
    next(error);
  }
}

/**
 * 添加分类
 * @param {*} req
 * @param {*} res
 * @param {*} next
 */
async function insertCategory(req, res, next) {
  if (validateParams({ req, res, validationResult })) return;
  try {
    const { cate_name } = req.body;
    const select_category_sql = `select cate_name from category where cate_name=?`;
    let [list] = await poolPromise.query(select_category_sql, [cate_name]);
    if (list.length) {
      return res.status(CODE_Conflict).json({
        code: CODE_Conflict,
        msg: "分类名已存在",
        data: null,
      });
    }
    const insert_category_sql = `insert into category(cate_name) values(?)`;
    let [{ affectedRows: cate_affectedRows }] = await poolPromise.execute(insert_category_sql, [
      cate_name,
    ]);
    if (!cate_affectedRows) {
      return res.status(500).json({
        code: 500,
        msg: "添加分类失败",
        data: null,
      });
    }
    res.status(200).json({
      code: 200,
      msg: "添加分类成功",
    });
  } catch (error) {
    next(error);
  }
}

/**
 * 删除分类
 * @param {*} req
 * @param {*} res
 * @param {*} next
 */
async function deleteCategory(req, res, next) {
  if (validateParams({ req, res, validationResult })) return;
  try {
    const { ids } = req.body;
    const delete_category_sql = `delete from category where cate_id in (${new Array(ids.length)
      .fill("?")
      .join(",")})`;
    let [{ affectedRows: cate_affectedRows }] = await poolPromise.execute(delete_category_sql, ids);
    if (!cate_affectedRows) {
      return res.status(500).json({
        code: 500,
        msg: "删除分类失败",
        data: null,
      });
    }

    res.status(200).json({
      code: 200,
      msg: "删除分类成功",
      data: null,
    });
  } catch (error) {
    next(error);
  }
}

/**
 * 修改分类
 * @param {*} req
 * @param {*} res
 * @param {*} next
 */
async function updateCategory(req, res, next) {
  if (validateParams({ req, res, validationResult })) return;
  try {
    const { cate_id, cate_name } = req.body;
    const update_category_sql = `update category set cate_name = ? where cate_id = ?`;
    let [{ affectedRows: cate_affectedRows }] = await poolPromise.execute(update_category_sql, [
      cate_name,
      cate_id,
    ]);
    if (!cate_affectedRows) {
      return res.status(500).json({
        code: 500,
        msg: "修改分类失败",
        data: null,
      });
    }
    res.status(200).json({
      code: 200,
      msg: "修改分类成功",
      data: null,
    });
  } catch (error) {
    next(error);
  }
}

module.exports = {
  selectCategoryList,
  insertCategory,
  deleteCategory,
  updateCategory,
};
